﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCDishList : System.Web.UI.UserControl
    {
        private IDishService _dishService;
        
        public static List<RedHouseDish> Dishes { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            cboCategory.SelectedIndexChanged += new EventHandler(cboCategory_SelectedIndexChanged);
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {            
            Guid categoryId = new Guid(cboCategory.SelectedValue);
            rptDish.DataSource = Dishes.Where(d => d.CategoryID == categoryId).OrderBy(c => c.DisplayOrder).ToList();
            rptDish.DataBind();
        }

        private void Initialize()
        {
            _dishService = new DishService();
            Dishes = _dishService.GetAllDish().OrderBy(d => d.Title).ToList();
            cboCategory.DataSource = CategoryService.Instance.GetAll().Where(c => c.Group != RedHouseGroup.GroupList[0].Value).OrderBy(c=>c.DisplayOrder).ToList();
            cboCategory.DataTextField = "Title";
            cboCategory.DataValueField = "Id";
            cboCategory.DataBind();

            cboCategory.SelectedIndex = 0;
            Guid categoryId = new Guid(cboCategory.SelectedValue);
            rptDish.DataSource = Dishes.Where(d => d.CategoryID == categoryId).OrderBy(d=>d.DisplayOrder).ToList();
            rptDish.DataBind();
        }
    }
}